
import komp.Suwak
class komp.Lista extends MovieClip {
	var _rowsWidth = 140
	var _content:MovieClip;
	var _tab:Array;
	var _rowCount = 4;
	var maska;
	var array;
	var _onAddItem:Function;
	var _onPress:Function;
	var _onRollOver:Function;
	var _onRollOut:Function;
	var _onIni:Function
	var _kontener:MovieClip;
	var kontener
	var mc_tlo:MovieClip
	var _maska
	var _odstep_y=0
	var __suwak:Suwak
	var __current:MovieClip  ////aktualnie wcisniety guzik na liscie
	var __linkage:String="list_tf"
	////formatowanie
	var format_out:Object= {size:11,color:0x000000}  ///formatowanie wiersza
	var format_rol:Object= {size:11,color:0xFFFFFF}  ///formatowanie wiersza
	var kolor_pasek="0x666666"


	function set onAddItem(f) {
		_onAddItem = f;
	}
	function set onPress_(f) {
		_onPress = f;
	}
	function set onRollOut_(f) {
		_onRollOut = f;
	}
	function set onRollOver_(f) {
		_onRollOver = f;
	}
	function set linkage(s:String){
		__linkage=s
	}


	function Lista() {}

	function removeAll(){
	  for(var i in array){
             removeItemAt(i)
	}
	}

	function removeItemAt(index:Number){
		var mc:MovieClip=array.splice(index,1)
		eval(mc).removeMovieClip()
		ustaw()
	}

	private function handler_onPress(mc:MovieClip){
	_onPress(mc)
	}

	private function handler_onRollOver(mc:MovieClip){
formatuj_wiersz(mc,format_rol)
		mc.tlo._alpha = 100;
	_onRollOver(mc)
	}

	private function handler_onRollOut(mc:MovieClip){
		formatuj_wiersz(mc,format_out)
		mc.tlo._alpha = 0;
	_onRollOut(mc)
	}


	function addItem(ob:Object) {
		var p = _content.getNextHighestDepth();
		var mc:MovieClip = _content.createEmptyMovieClip("row"+p, p);
		mc.__this=this
		mc.onPress=function(){
			this.__this.__current=this
			this.__this.handler_onPress(this)
		}
		mc.onRollOver=Delegate2.create(this,handler_onRollOver,mc)
		mc.onRollOut=Delegate2.create(this,handler_onRollOut,mc)
		array.push(mc);
		mc.selectedItem = ob;


            if(__linkage==undefined){   ////rysujemy pole tekstowe w as
		var poj=mc.createEmptyMovieClip("list_tf",1)
		var tf = poj.createTextField("t", 1, 0,_odstep_y, _rowsWidth, 1);
		mc.tf=tf
		tf.autoSize = "left"
		}else{   ///zaciagamy pole tekstowe z biblioteki
		var tf=(mc.attachMovie(__linkage,__linkage,1)).t
		mc.tf=tf
		tf._y=_odstep_y
		tf.multiline=false
		tf._width=_rowsWidth
		}


		///tf.border=true
		tf.selectable = false;

		tf.html = true;
		tf.htmlText = ob.label



		///format
		formatuj_wiersz(mc,format_out)
		////tlo
		var m = mc.createEmptyMovieClip("tlo", mc.t.getDepth()-1);
		////Rysowanie.prostokat(m, 0, 0, this._rowsWidth-1, tf._height+tf._y*2, 1, 1, kolor_pasek, 100);
		Drawing.rectangle(m,0,0,this._rowsWidth-1,tf._height+tf._y*2,[kolor_pasek,100])
		m._alpha = 0;
		///
		///hitArea
		var area=mc.createEmptyMovieClip("mc_area",200)
		//Rysowanie.prostokat(area,0,0,_rowsWidth,tf._height+tf._y*2,1,1,0x000000,100)
		Drawing.rectangle(area,0,0,_rowsWidth,tf._height+tf._y*2,[0x000000,100])
		mc.hitArea=area
	      area._visible=0


		ustaw();
		_onAddItem(mc)
		return mc;
	}

	private function formatuj_wiersz(mc:MovieClip,typ:Object){
          	var formatuj = new TextFormat();
		for(var j in typ){
		formatuj[j]=typ[j]
		}
		mc.tf.setTextFormat(formatuj);
	}


	function ustaw() {
		var ost;
		for (var i = 0; i<array.length; i++) {
			var mc = array[i];
			mc._y = (i == 0) ? 0 : Math.round(ost.getBounds(ost._parent).yMax)
			mc.selectedIndex=i
			ost = mc;
		}
	}




	function dataProvider(ch) {
		setDataProvider(XML_.getArrayObject(ch))
	}

	 function setDataProvider(tablica:Array){
		_kontener = this.createEmptyMovieClip("kontener",1)
		_content = _kontener.createEmptyMovieClip("kontener2", 1);
		array = [];
		_tab =tablica
		for (var i = 0; i<_tab.length; i++) {
			var row = addItem(_tab[i]);
		}
		maskuj(row._height,i);
		create_tlo()
		////
		onProviderEnd()

	}


	private function onProviderEnd(){

	}

	function setScrollPosition(wartosc){
                __suwak.setScrollPosition(wartosc)
	}



	function create_tlo(){
		_kontener.attachMovie("lista_tlo","mc_tlo",-100)
	      _kontener.mc_tlo._width=_rowsWidth
	 _kontener.mc_tlo._height=_maska._height
	}

	function maskuj(wys:Number, il:Number) {
		_maska= _kontener.createEmptyMovieClip("maska", 10);
		var _wysokosc=(il*wys)
		////Rysowanie.prostokat(_maska, 0, 0, _rowsWidth, _wysokosc, 1, 1, "0xFF0000", 10);
		Drawing.rectangle(_maska, 0, 0, _rowsWidth, _wysokosc,["0xFF0000",10])
		_content.setMask(_maska)
		if (il>_rowCount) {
			var _wysokosc=_rowCount*wys
			_maska._height=_wysokosc
			wrzucSuwak(_wysokosc);
		}
	}
	function wrzucSuwak(wys) {
		var s = _kontener.attachMovie("suwak", "suwak", 100);
		__suwak=s
		var stosunek=(_kontener.maska._height/_content._height)
		///trace("stosunek = "+stosunek)
		s.height = wys;
		s.y = 0;
		s.x = _rowsWidth-s._width-2
		s.klip = _content;
		s.zakres = [0, -_content._height+_kontener.maska._height,stosunek];
		s.onChange = function(w) {
			s.klip._y = w;
		};
	}
	///////////////////////////
}
